﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.ParentForm;
using Components;
using UI.Lib.Factory;
using System.Collections;
using UI.Lib;

namespace UI.Sysset
{
    public partial class FrmUserRole : BaseMedit
    {
        ToolStripButton tspTs = null;
        ToolStripButton tspRefresh = null;

        public FrmUserRole(string modulecode, string action)
            : base(modulecode, action)
        {
            InitializeComponent();

            //回写的方法
            this.SaveCallFuntion = "Sys0a02Manager$SaveData"; //保存的方法
            IsAfterSaveCloseWin = false;
            this.allowDtlEmpty = true; //可能用户一次想把全部的记录删除
            this.isAutoSaveDelRow = false;
        }

        protected override void PageInit()
        {
            tspTs = new ToolStripButton("提取明细");
            tspTs.DisplayStyle = ToolStripItemDisplayStyle.Text;
            tspTs.Name = "tspTs";
            this.tspDtlTop.Items.Insert(0, tspTs);
            tspTs.Click += new EventHandler(tspTs_Click);

            //添加刷新按钮
            this.tspTop.Items.Add(new ToolStripSeparator());
            tspRefresh = new ToolStripButton("刷 新");
            tspRefresh.DisplayStyle = ToolStripItemDisplayStyle.Text;
            tspRefresh.Name = "tspRefresh";
            this.tspTop.Items.Add(tspRefresh);
            tspRefresh.Click += new EventHandler(tspRefresh_Click);
        }

        //提取明细
        void tspTs_Click(object sender, EventArgs e)
        {
            string rolecode = Validation.ToString(this.tbSummary.GetValue("rolecode"));
            if (string.IsNullOrEmpty(rolecode))
            {
                MessageBox.Show("角色编码不能为空！", "提示");
                this.tbSummary.Focus("rolecode");
                return;
            }

            this.tbSummary.Enable("rolecode", false);
            tspTs.Enabled = false;

            DataTable dt = CommonHelper.Entity.ExecFun("getlistbyrolecode", new KeyValuePair<string, object>("@rolecode", rolecode));
            if (null == dt || 0 == dt.Rows.Count) return;

            this.dgvDtl.DataSource = null;

            DataGridViewRow dgr = null;
            foreach (DataRow dr in dt.Rows)
            {
                dgr = new DataGridViewRow();
                dgr.CreateCells(this.dgvDtl);

                this.dgvDtl.Rows.Add(dgr);
                //填充值
                UI.Lib.UIHelper.FillDataGridViewRowFromDataRow(dr, this.dgvDtl.Rows[this.dgvDtl.Rows.Count - 1]);
            }

            this.dgvDtl.Rows[this.dgvDtl.Rows.Count - 1].Cells[0].Selected = true; //选择新行
        }

        //刷新
        void tspRefresh_Click(object sender, EventArgs e)
        {
            this.tbSummary.Clear();
            this.tbSummary.Enable("rolecode", true);
            tspTs.Enabled = true;

            this.dgvDtl.Rows.Clear();
        }

        protected override System.Collections.Hashtable SaveOtherData()
        {
            Hashtable tb = new Hashtable();
            tb["hastake"] = tspTs.Enabled ? "0" : "1"; //记录用户是否点击了提取明细
            return tb;
        }

        protected override void AfterSaveData(bool isSuccess)
        {
            if (isSuccess)
            {
                tspTs.Enabled = true; //恢复提取明细按钮可用
                this.tbSummary.Enable("rolecode", true);
            }
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            if ("rolecode" == name) //限定明细中的角色必须为本公司的
            {
                if (!Context.IsSuperUser)
                {
                    where = string.Format(" (comcode = '{0}' OR comcode IS NULL) ", Context.User.Comcode);
                }
            }

            return true;
        }

        protected override bool BeforeDtlPopUp(string name, ref string where, ref string[] others)
        {
            if ("usercode" == name) //限定明细中的用户名必须为本公司的
            {
                if (!Context.IsSuperUser)
                {
                    where = string.Format(" (comcode = '{0}' OR comcode IS NULL) ", Context.User.Comcode);
                }
            }
            return true;
        }
    }
}
